package com.youmingxing.activity.selfInfo;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.TextView;

import com.youmingxing.R;
import com.youmingxing.activity.BaseActivity;
import com.youmingxing.adpter.selfInfo.MyFansAdapter;
import com.youmingxing.adpter.selfInfo.MyFansTopAdapter;
import com.youmingxing.bean.selfinfo.FansOrFocus;
import com.youmingxing.network.AllStarApi;
import com.youmingxing.network.BaseJsonHandler;
import com.youmingxing.network.SelfInfoApi;
import com.youmingxing.utils.DisplayUtil;
import com.youmingxing.view.DialogManager;
import com.youmingxing.view.RefreshListView;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;


/**
 * @author evilyin(ChenZhixi)
 * @since 2015-9-1
 * 我的粉丝
 */

public class FansActivity extends BaseActivity implements RefreshListView.OnLoadMoreListener {

    private RefreshListView listView;
    View headView;
    private GridView topGriView;
    private MyFansAdapter adapter;
    private MyFansTopAdapter topAdapter;

    private List<FansOrFocus> myFansBeanList = new ArrayList<>();
    private int mark = 1;
    private int userId = -1;
    private int type; //type = 0;金牌粉丝 type =1 加油票赠送排行榜
    public static final String EXTRAS_ID = "user_id";
    public static final String EXTRAS_TYPE = "type";
    public static final int TYPE_FANS = 0;
    public static final int TYPE_TICKET_RANK = 1;
    private boolean hasTop = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        BaseSetContentView(R.layout.activity_list_view);
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            userId = bundle.getInt(EXTRAS_ID, -1);
            type = bundle.getInt(EXTRAS_TYPE, TYPE_FANS);
        }
        DialogManager.showDialog(this);
        listView = (RefreshListView) findViewById(R.id.list_view);
        getDataFromServer();
    }

    public void getDataFromServer() {
        if (type == TYPE_FANS) {
            setTopbarName(R.string.myfans);
            if (userId != -1) {
                //他人的粉丝
                SelfInfoApi.getUserFans(mark, userId, fansHandler);
            } else {
                //我的粉丝
                SelfInfoApi.getFans(mark, fansHandler);
            }
        } else if (type == TYPE_TICKET_RANK) {
            //投票榜
            setTopbarName("加油票赠送榜");
            AllStarApi.getUserVoters(userId, mark, fansHandler);
        }
    }

    @Override
    public void onErrorClick() {
        getDataFromServer();
    }

    private void initView() {
        headView = getLayoutInflater().inflate(R.layout.activity_myfans_top, null);
        topGriView = (GridView) headView.findViewById(R.id.fans_top_gridview);
        listView.setOnLoadMoreListener(this);
        myFansBeanList = new ArrayList<>();
        adapter = new MyFansAdapter(FansActivity.this);
        listView.setAdapter(adapter);
        topAdapter = new MyFansTopAdapter(this, myFansBeanList);
        topGriView.setAdapter(topAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (id < 0) {
                    return;
                }
                int realPosition = (int) id;
                if (hasTop) {
                    realPosition = (int) id + 3;
                }
                Intent intent = new Intent(FansActivity.this, PersonalInfoActivity.class);
                intent.putExtra("u_id", myFansBeanList.get(realPosition).id);
                startActivity(intent);
            }
        });
    }

    BaseJsonHandler fansHandler = new BaseJsonHandler(this) {
        @Override
        public void onSuccessWithData(JSONObject response) {
            listView.endLoadMore();
            showErrorPage(false);
            if (topGriView == null) {
                initView();
            }
            JSONArray data = response.optJSONArray("data");
            for (int i = 0; i < data.length(); i++) {
                FansOrFocus myFansBean = new FansOrFocus(data.optJSONObject(i));
                myFansBeanList.add(myFansBean);
            }
            if (mark == 1) {
                //第一页
                if (data.length() == 0) {
                    View v = View.inflate(FansActivity.this, R.layout.footer_no_data_focus, null);
                    v.setLayoutParams(new AbsListView.LayoutParams(DisplayUtil.getScreenWidth
                            (), DisplayUtil.getScreenHeightWithoutHeader()));
                    ((TextView) v.findViewById(R.id.hin1)).setText("您还没有任何粉丝呢");
                    ((TextView) v.findViewById(R.id.hin2)).setText("快去发表作品吧~");
                    listView.noData(v);
                } else if (data.length() < 20) {
                    //以列表形式展现,没有top部分
                    adapter.setData(myFansBeanList);
                    listView.noMoreData();
                } else {
                    //大于20,有top3
                    listView.addHeaderView(headView);
                    List<FansOrFocus> myFansFrom4List = myFansBeanList.subList(3, myFansBeanList
                            .size());
                    topAdapter.notifyDataSetChanged();
                    adapter.setStartFrom4(true);
                    hasTop = true;
                    adapter.setData(myFansFrom4List);
                }
            } else {
                //非第一页
                if (data.length() == 0) {
                    listView.noMoreData();
                }
                adapter.setData(myFansBeanList.subList(3, myFansBeanList.size()));
            }
            mark++;
        }

        @Override
        public void onFail(int statusCode, String statusText) {
            super.onFail(statusCode, statusText);
            if (myFansBeanList == null || myFansBeanList.size() <= 0) {
                showErrorPage(true);
            }
            listView.endLoadMore();
        }
    };

    @Override
    public void onLoadMore() {

        if (type == TYPE_FANS) {
            if (userId != -1) {
                //他人的粉丝
                SelfInfoApi.getUserFans(mark, userId, fansHandler);
            } else {
                //我的粉丝
                SelfInfoApi.getFans(mark, fansHandler);
            }
        } else if (type == TYPE_TICKET_RANK) {
            AllStarApi.getUserVoters(userId, mark, fansHandler);
        }

    }
}
